package com.bdqn.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.pojo.Role;
import com.bdqn.pojo.User;
import com.bdqn.service.RoleService;
import com.bdqn.service.UserService;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    Logger logger= LoggerFactory.getLogger(getClass());
    @Resource
    private UserService service;
    @Resource
    private RoleService roleService;

    @GetMapping("toLogin")
    public String toLogin(){

        return "login";
    }

    @PostMapping("doLogin")
    public String doLogin(Model model, String usrName, String usrPassword, HttpSession session){
        User user=service.login(usrName,usrPassword);
        if (user!=null){
            session.setAttribute("loginUser",user);
            return "main";
        }
        model.addAttribute("message","账号或密码错误!");
        return "login";
    }

    @RequestMapping("user/list")
    public String toList(Model model, String usrName, Long roleId, @RequestParam(defaultValue = "1")Long pageIndex){
        List<Role> role=roleService.list();
        Page page=new Page();
        page.setCurrent(pageIndex);
        User user=new User();
        user.setUsrName(usrName);
        user.setUsrRoleId(roleId);
        IPage iPage=service.getPage(page,user);
        model.addAttribute("roles",role);
        model.addAttribute("usrName",usrName);
        model.addAttribute("roleId",roleId);
        model.addAttribute("userPager", iPage);
        return "user/list";
    }

    @GetMapping("user/add")
    public String toAdd(Model model){
        List<Role> role=roleService.list();
        model.addAttribute("roles",role);
        return "user/add";
    }

    @PostMapping("user/doAdd")
    public String doAdd(User user){
        user.setUsrPassword(service.save(user.getUsrPassword()));
        boolean bo=service.save(user);
        if (bo){
            return "redirect:list";
        }
        return "user/add";
    }

    @GetMapping("user/edit")
    public String toEdit(Model model,Integer usrId ){
        User user=service.getById(usrId);
        List<Role> role=roleService.list();
        model.addAttribute("roles",role);
        model.addAttribute("sysUser",user);
        return "user/edit";
    }

    @PostMapping("user/save")
    public String doSave(User user){
        user.setUsrPassword(service.save(user.getUsrPassword()));
        boolean bo=service.updateById(user);
        if (bo){
            return "redirect:list";
        }
        return "user/edit";
    }

    @GetMapping("user/del/{usrId}")
    @ResponseBody
    public Map doDel(@PathVariable("usrId")Integer usrId){
        boolean bo=service.removeById(usrId);
        Map map=new HashMap();
        if (bo){
            map.put("result","true");
        }
        return map;
    }
}
